package com.qianbao.qianbaobusiness.ui.push;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.qianbao.qianbaobusiness.json.MessageListBean;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class MessageRxPresent extends RxMvpPresenter<MessageView> {

    MyApi mMyApi;

    @Inject
    public MessageRxPresent(MyApi myApi) {
        mMyApi = myApi;
    }

    public void getUserMsgList(String userNo, String userChannel, String pos, String limit, String type) {
        Map<String, String> itemMap = new HashMap<>();
        itemMap.put("userNo", userNo);
        itemMap.put("userChannel", userChannel);
        itemMap.put("pos", pos);
        itemMap.put("limit", limit);
        itemMap.put("type", type);
        mMyApi.getUserMsgList(itemMap)
                .compose(RxUtils.<Data<MessageListBean>>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<MessageListBean>>() {
                    @Override
                    public void onNext(Data<MessageListBean> messageListBeanData) {
                        if (RxObserver.checkJsonCode(messageListBeanData, true)) {
                            getView().onMessageListData(messageListBeanData.getResult(), true);
                        } else {
                            getView().onMessageListData(null, false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().onMessageListData(null, false);
                    }
                });
    }

    public void setMessageRead(String userNo, String userChannel, String messageNos) {
        Map<String, String> itemMap = new HashMap<>();
        itemMap.put("userNo", userNo);
        itemMap.put("userChannel", userChannel);
        itemMap.put("messageNos", messageNos);
        mMyApi.setMessageRead(itemMap)
                .compose(RxUtils.<Data>applySchedulersLifeCycle(getView()))
                .subscribe();
    }

    public void setMessageDelet(String userNo, String userChannel, String messageNos) {
        Map<String, String> itemMap = new HashMap<>();
        itemMap.put("userNo", userNo);
        itemMap.put("userChannel", userChannel);
        itemMap.put("messageNos", messageNos);
        mMyApi.setMessageDelet(itemMap)
                .compose(RxUtils.<Data>applySchedulersLifeCycle(getView()))
                .subscribe();
    }
}
